package control.micro
{
	import avmplus.getQualifiedClassName;
	import events.BattleEvent;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import model.macro.Unit;
	import model.micro.ArcherUnit;
	import model.micro.Battle;
	import model.micro.FieldUnit;
	import model.micro.RangedFieldUnit;
	import view.micro.BattleSprite;
	
	/**
	 * The controller object for a human player defending in a battle.
	 */
	public class DefendingHumanBattleController extends HumanBattleController
	{
		static private const K_DEPLOY:Number = 32;
		static private const K_FIRE:Number = 90;
		static private const K_ACTIVATE_1:Number = 81;
		static private const K_ACTIVATE_2:Number = 87;
		
		private var selected:FieldUnit;
		
		/**
		 *
		 * @param	battle			The battle this is controlling.
		 * @param	battleSprite	The sprite creating the events.
		 */
		public function DefendingHumanBattleController(battle:Battle, stage:Stage)
		{
			super(battle, stage);
			
			battle.addEventListener(BattleEvent.UNIT_SELECTED, unitSelected);
			
			for each (var unit:FieldUnit in battle.defense_field_units)
			{
				addUnitController(unit);
			}
		}
		
		private function unitSelected(e:Event):void
		{
			if (!(e is BattleEvent))
			{
				throw new Error("Not a battle event: " + getQualifiedClassName(e) + e.toString());
			}
			selected = ((e as BattleEvent).data as FieldUnit);
		}
		
		/**
		 * Update every frame.
		 */
		override public function update():void
		{
			for (var i:int = unit_controllers.length - 1; i >= 0; i--)
			{
				if (unit_controllers[i].readyToRemove())
				{
					unit_controllers.splice(i, 1);
				}
				else
				{
					unit_controllers[i].update();
				}
				
			}
			
			// TODO: Optional Auto-Fire arrows
			if (keys[K_FIRE])
			{
				var angle:Number = Math.atan2(mouse_position.y - battle.fortress.getBoundingBox().top, mouse_position.x - battle.fortress.getBoundingBox().right);
				for each (var archer:ArcherUnit in battle.archers)
				{
					archer.fire(battle, angle);
				}
			}
		}
		
		/**
		 * Handle keydown events.
		 * @param	e	the event
		 */
		override protected function onKeyDown(e:KeyboardEvent):void
		{
			super.onKeyDown(e);
			
			switch (e.keyCode)
			{
				case K_DEPLOY: 
					if (battle.garrison.length > 0)
					{
						var unit:FieldUnit = battle.deployGarrisonedUnit(battle.garrison[0]);
						addUnitController(unit);
					}
					break;
				case K_ACTIVATE_1: 
					activateSelectedAbility(0);
					break;
				case K_ACTIVATE_2: 
					activateSelectedAbility(1);
					break;
			}
		}
		
		override protected function onMouseUp(e:MouseEvent):void
		{
			super.onMouseUp(e);
			
			selected = null;
		}
		
		private function activateSelectedAbility(number:int):void
		{
			if (selected != null && (battle.defense_field_units.indexOf(selected) != -1))
			{
				selected.activateAbility(number);
			}
		}
	
	}

}